package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.administrator.zhongyichou.Bean.JoinActionBean;
import com.example.administrator.zhongyichou.adapter.JoinActionRecyclerAdapter;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.JoinActionHttpLoader;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.net.protocol.JoinActionProtocol;
import com.example.administrator.zhongyichou.net.protocol.MillionsJoinProtocol;
import com.example.administrator.zhongyichou.utils.IdCard;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/23.
 * 描述：加入行动
 */

public class JoinActionActivity extends BaseActivity implements JoinActionRecyclerAdapter.OnDeleteClick {

    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.btn_add)
    Button btnAdd;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    private JoinActionRecyclerAdapter adapter;
    private List<JoinActionBean> list = new ArrayList<>();
    private int mutualType = 0;//0加入互助跳转过来的，1加入百万互助跳转过来的

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
        mutualType = getIntent().getIntExtra("mutual_type", 0);
        list.add(new JoinActionBean());
        adapter = new JoinActionRecyclerAdapter(this, list);
        adapter.setOnDeleteClick(this);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter);
        recycler.setNestedScrollingEnabled(false);
    }

    @Override
    protected void responseData(GsonBaseProtocol mGsonBaseProtocal) {

    }

    @Override
    protected HttpLoader initHttpLoader() {
        return null;
    }

    @Override
    protected int initContentView() {
        return R.layout.join_action_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    private void initHead() {
        headerview.setTitle("加入行动");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @OnClick({R.id.btn_add, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add://添加一项
                list.add(new JoinActionBean());
                adapter.notifyDataSetChanged();
                break;
            case R.id.btn_commit:
                addMember();
                break;
        }
    }

    private void addMember() {
        JSONArray array = new JSONArray();
        for (JoinActionBean bean : list) {
            JSONObject object = new JSONObject();
            object.put("member_name", bean.name);
            object.put("member_num", bean.cardID);
            object.put("type_id", getType(bean.cardID));
            array.add(object);
        }
        JoinActionHttpLoader httpLoader = new JoinActionHttpLoader(JoinActionProtocol.class);
        Map<String, String> map = new HashMap<>();
        Map<String, JSONArray> map1 = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        map1.put("members", array);
        httpLoader.setParams(NetConfig.ADD_MEMBER, map, map1);
        httpLoader.showDialog(this, true, "正在加入");
        httpLoader.executor(new ResultListener<JoinActionProtocol>() {
            @Override
            public void success(JoinActionProtocol mGsonBaseProtocol) {
                if (mutualType == 0) {
                    Intent intent = new Intent(JoinActionActivity.this, WaitPayActivity.class);
                    intent.putExtra("member_id", mGsonBaseProtocol.getData().getMember_id());
                    intent.putExtra("mutual_type", mutualType);
                    startActivity(intent);
                    finish();
                } else {
                    String[] ids = mGsonBaseProtocol.getData().getMember_id().split(",");
                    List<MillionsJoinProtocol.DataBean> dateList = new ArrayList<MillionsJoinProtocol.DataBean>();
                    for (int i = 0; i < list.size(); i++) {
                        JoinActionBean bean = list.get(i);
                        MillionsJoinProtocol.DataBean dataBean = new MillionsJoinProtocol.DataBean();
                        dataBean.setSelect(true);
                        dataBean.setMember_name(bean.name);
                        dataBean.setMember_num(bean.cardID);
                        dataBean.setMember_id(ids[i]);
                        dateList.add(dataBean);
                    }
                    EventBus.getDefault().post(new BaseEventBean<>(Constants.TYPE_5, dateList));
                    finish();
                }
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    //删除某一项
    @Override
    public void onDeleteClick(int position) {
        list.remove(position);
        adapter.notifyDataSetChanged();
    }

    private String getType(String idCard) {
        int age = IdCard.getAgeByIdCard(idCard);
        if (age < 18) {
            return "2";
        } else if (age < 51) {
            return "3";
        } else if (age < 66) {
            return "4";
        }
        return "-1";
    }
}